The Java Part
After setting up the the Maven pom.xml file as shown in the previous section, we can continue with implementing a basic JSR-286 compatible portlet. Simplest Hello World Portlet from the GateIn Quickstarts collection shows the very essence of every JSR-286 Portlet:
SimplestHelloWorldPortlet.java
package org.jboss.portal.portlet.samples;
import java.io.IOException;
import java.io.PrintWriter;
import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
/**
* The simplest posible Portlet.
*
* @author Peter Palaga
*/
public class SimplestHelloWorldPortlet extends GenericPortlet {
/**
* Serves the VIEW mode. Writes "Hello World !" to the response writer.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
@Override
public void doView(RenderRequest request, RenderResponse response) throws IOException {
PrintWriter writer = response.getWriter();
writer.write("Hello World !");
writer.close();
}
}
As you can see in the listing, we have done two important things:
In this simplest portlet variant we have decided not to support edit and help portlet modes. To add them, just override the doEdit() and doHelp from javax.portlet.GenericPortlet and configure the portlet.xml file accordingly.
portlet.xml
The portlet.xml file for a plain JSR 286 portlet is pretty straightforward:
portlet.xml
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
<portlet>
<description>Simplest Hello World Portlet is the very essence of every possible Portlet.</description>
<portlet-name>SimplestHelloWorldPortlet</portlet-name>
<display-name>Simplest Hello World Portlet</display-name>
<portlet-class>org.jboss.portal.portlet.samples.SimplestHelloWorldPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<!-- You can uncomment the other modes when your portlet-class supports them
<portlet-mode>edit</portlet-mode>
<portlet-mode>help</portlet-mode>
-->
</supports>
<portlet-info>
<title>Simplest Hello World Portlet</title>
</portlet-info>
</portlet>
</portlet-app>
web.xml
There is no need to configure any filters, servlets, mappings, etc. for a plain JSR 286 portlet to work but maven-war-plugin by default requires the web.xml. To solve it, just include a web.xml file which contains solely the root element like the following:
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--
There is no need to configure any filters, servlets, mapping & co. for this demo to work
but maven-war-plugin by default requires this file.
-->
</web-app>